#include "CampaignEditForm.h"
#include "ui_CampaignEditForm.h"
#include "DataViewDelegate.h"


#include <ItemReference.h>
#include <CampaignItem.h>



CampaignEditFormBase::CampaignEditFormBase(ItemRoot *rootItem, QWidget *parent) :
    QWidget(parent),
    BaseEditForm(rootItem, new D20CampaignModel(this)),
    ui(new Ui::CampaignEditForm)
{
    initialize(this);

    ui->setupUi(this);

    // add actions
    addAction(ui->actionDelete);

    // setup tree view
    ui->treeView->setModel(m_model);
    ui->treeView->setItemDelegate(new DataViewDelegate(this));
    ui->treeView->header()->setResizeMode(QHeaderView::ResizeToContents);

    // setup line edit
    ui->lineEditName->setText(root()->name());

}



CampaignEditFormBase::~CampaignEditFormBase()
{
    delete ui;
}



void CampaignEditFormBase::updateData()
{
    on_lineEditName_editingFinished();
}


QString CampaignEditFormBase::message() const
{
    if (!root()) return tr("Invalid Campaign!");
    return QString("%1 - %2").arg(root()->name()).arg(root()->filePath());
}




#if 0
void CampaignEditFormBase::initialize()
{
    //m_model->setRootItem(root());


    ui->treeView->hideColumn(AbstractItem::IdData);
    ui->treeView->hideColumn(AbstractItem::CategoryData);

    if (!root()) return;

    ui->lineEditName->setText(root()->name());
}


void CampaignEditFormBase::on_actionAdd_Group_triggered()
{
    QAbstractItemModel* model = ui->treeView->model();
    if (!model) return;

    model->insertRows(model->rowCount(QModelIndex()), 1, QModelIndex());
    ui->treeView->setCurrentIndex(model->index(model->rowCount(QModelIndex())-1,0,QModelIndex()));
}


void CampaignEditFormBase::on_actionAdd_Item_triggered()
{
    QAbstractItemModel* model = ui->treeView->model();
    if (!model) return;

    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid()) return;

    QVariant category = index.sibling(index.row(), AbstractItem::CategoryData).data(Qt::EditRole);
    if (!category.isValid()) return;

    QModelIndex parent;
    switch (category.toInt())
    {
    case ItemCategory_Root: return;
    case ItemCategory_Group: { parent = index; break; }
    case ItemCategory_Item:  { parent = index.parent(); break; }
    case ItemCategory_Reference: { parent = index.parent().parent(); break; }
    default: return;
    }

    model->insertRows(model->rowCount(parent), 1, parent);
    ui->treeView->setCurrentIndex(model->index(model->rowCount(parent)-1,0,parent));
}



void CampaignEditFormBase::on_actionAdd_Reference_triggered()
{
    QAbstractItemModel* model = ui->treeView->model();
    if (!model) return;

    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid()) return;

    QVariant category = index.sibling(index.row(), AbstractItem::CategoryData).data(Qt::EditRole);
    if (!category.isValid()) return;

    QModelIndex parent;
    switch (category.toInt())
    {
    case ItemCategory_Item:         { parent = index; break; }
    case ItemCategory_Reference:    { parent = index.parent(); break; }

    case ItemCategory_Root:         return;
    case ItemCategory_Group:        return;
    default: return;
    }

    model->insertRows(model->rowCount(parent), 1, parent);
    ui->treeView->setCurrentIndex(model->index(model->rowCount(parent)-1,0,parent));
}
#endif


void CampaignEditFormBase::on_actionDelete_triggered()
{
    QAbstractItemModel* model = ui->treeView->model();
    if (!model) return;

    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid()) return;

    model->removeRows(index.row(), 1, index.parent());
    //ui->treeView->setCurrentIndex(model->index(model->rowCount(parent)-1,0,parent));
}



void CampaignEditFormBase::on_lineEditName_editingFinished()
{
    if (!root()) return;

    root()->setData(AbstractItem::NameData, ui->lineEditName->text());
}

void CampaignEditFormBase::addItem(const int& group)
{
    ui->treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
    int row = m_model->addItem(group, QString("%1 %2").arg(tr("New")).arg(m_model->groupName(group)));
    QModelIndex index = m_model->index(row, AbstractItem::NameData, m_model->groupIndex(group));


    if (!index.isValid()) return;
    ui->treeView->setCurrentIndex(index);
    ui->treeView->edit(index);
}


void CampaignEditFormBase::on_actionAdd_Attribute_triggered()
{
    addItem(D20CampaignModel::AttributeGroup);
}


void CampaignEditFormBase::on_actionAdd_Skill_triggered()
{
    addItem(D20CampaignModel::SkillGroup);
}


void CampaignEditFormBase::on_actionAdd_Feat_triggered()
{
    addItem(D20CampaignModel::FeatGroup);
}


void CampaignEditFormBase::on_actionAdd_Special_Ability_triggered()
{
    addItem(D20CampaignModel::SpecialGroup);
}


void CampaignEditFormBase::on_actionAdd_Bonus_triggered()
{
    addItem(D20CampaignModel::BonusGroup);
}


void CampaignEditFormBase::on_actionAdd_Bonus_Level_triggered()
{
    addItem(D20CampaignModel::BonusLevelGroup);
}


